package com.ygkj.demo;

import android.content.Context;
import android.graphics.Color;
import android.os.Environment;
import android.os.storage.OnObbStateChangeListener;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.VideoView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.volokh.danylo.video_player_manager.manager.PlayerItemChangeListener;
import com.volokh.danylo.video_player_manager.manager.SingleVideoPlayerManager;
import com.volokh.danylo.video_player_manager.manager.VideoPlayerManager;
import com.volokh.danylo.video_player_manager.meta.MetaData;
import com.ygkj.demo.viewholder.ImageHolder;
import com.ygkj.demo.viewholder.TitleHolder;
import com.ygkj.demo.viewholder.VideoHolder;
import com.youth.banner.adapter.BannerAdapter;
import com.youth.banner.util.BannerUtils;

import java.io.File;
import java.util.List;

/**
 * 自定义布局,多个不同UI切换
 */
public class MultipleTypesAdapter extends BannerAdapter<DataBean, RecyclerView.ViewHolder> {
    private Context context;
    private SparseArray<RecyclerView.ViewHolder> mVHMap = new SparseArray<>();
    private  OnScrollPageListener onScrollPageListener;
    private VideoPlayerManager<MetaData> mVideoPlayerManager = new SingleVideoPlayerManager(new PlayerItemChangeListener() {
        @Override
        public void onPlayerItemChanged(MetaData metaData) {

        }
    });

    public MultipleTypesAdapter(Context context, List<DataBean> mDatas) {
        super(mDatas);
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case 1:
                return new ImageHolder(BannerUtils.getView(parent, R.layout.banner_image));
            case 2:
                Log.d("视屏","1");
                return new VideoHolder(BannerUtils.getView(parent, R.layout.banner_video));
            case 3:
                return new TitleHolder(BannerUtils.getView(parent, R.layout.banner_title));
            default:
                break;
        }
        return new ImageHolder(BannerUtils.getView(parent, R.layout.banner_image));
    }

    @Override
    public int getItemViewType(int position) {
        return getData(getRealPosition(position)).viewType;
    }

    @Override
    public void onBindView(RecyclerView.ViewHolder holder, DataBean data, int position, int size) {
        int viewType = holder.getItemViewType();
        switch (viewType) {
            case 1:
                ImageHolder imageHolder = (ImageHolder) holder;
                mVHMap.append(position, imageHolder);

//                imageHolder.imageView.setImageResource(data.imageRes);
                Glide.with(holder.itemView)
                        .load(data.imageUrl)
//                        .thumbnail(Glide.with(holder.itemView).load(R.drawable.ic_launcher_background))
                        .into(imageHolder.imageView);
                break;
            case 2:
                VideoHolder videoHolder = (VideoHolder) holder;
                mVHMap.append(position, videoHolder);
//                mVHMap.put(position,videoHolder);
//                videoHolder.player.setUp(data.imageUrl, true, null);
//                videoHolder.player.getBackButton().setVisibility(View.GONE);
//                videoHolder.player.startPlayLogic();
//                //增加封面
//                ImageView imageView = new ImageView(context);
//                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//                imageView.setImageResource(R.mipmap.ic_launcher);
//                videoHolder.player.setThumbImageView(imageView);
//                videoHolder.player.setLooping(true);
//                videoHolder.player.setIsTouchWiget(false);

//                if (!videoHolder.player.isPlaying()) {
//                    videoHolder.player.start();
//                }

//                mVideoPlayerManager.playNewVideo(null,videoHolder.player,data.imageUrl);
                videoHolder.player.setUp(Environment.getExternalStoragePublicDirectory("MIPS2").getPath()+ File.separator +"d4d443b1f5094693a45530d72bf11338/upload/video/640a6f54eb2a4ad1b694a3b67029c862.mp4","");
                videoHolder.player.startButton.setVisibility(View.GONE);
                videoHolder.player.startVideo();
                onScrollPageListener.OnScrollNumListener(position);

                break;
            case 3:
                TitleHolder titleHolder = (TitleHolder) holder;
                mVHMap.append(position, titleHolder);
                titleHolder.title.setText(data.title);
                titleHolder.title.setBackgroundColor(Color.parseColor(DataBean.getRandColor()));
                break;
            default:
                break;
        }
    }

    public SparseArray<RecyclerView.ViewHolder> getVHMap() {
        return mVHMap;
    }
    public void  setOnScrollPageListener(OnScrollPageListener onScrollPageListener){
        this.onScrollPageListener= onScrollPageListener;
    }

}
